package org.scohen.juploadr.uploadapi.flickrrest.ui;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.JUploadr;
import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.ui.MessageUtils;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.flickrrest.Messages;

public class NewPhotoSetDialog extends Dialog {
    private Button btnOk;
    private Button btnCancel;
    private Label lblDescription;
    private Label lblTitle;
    private Text txtDescription;
    private Text txtTitle;
    private Composite buttons;
    private Shell shell;
    private ImageAttributes primary;
    private List photosToAdd;
    private PhotoSet photoSet;

    public NewPhotoSetDialog(Shell shell, ImageAttributes primary) {
        super(shell);
        this.primary = primary;

    }

    public NewPhotoSetDialog(Shell window, List toAdd) {
        this(window, (ImageAttributes) toAdd.remove(0));
        photosToAdd = toAdd;
    }

    public void open() {
        Shell parent = getParent();

        GridData data;
        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText(Messages
                .getString("juploadr.flickrrest.ui.photoset.create.new")); //$NON-NLS-1$
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;

        GC gc = new GC(shell);
        int txtFieldWidth = gc.textExtent("B").x * 40; //$NON-NLS-1$
        int txtFieldHeight = gc.textExtent("B").y * 5; //$NON-NLS-1$
        gc.dispose();

        shell.setLayout(layout);
        lblTitle = new Label(shell, SWT.HORIZONTAL);
        lblTitle.setText(Messages
                .getString("juploadr.flickrrest.ui.photoset.title")); //$NON-NLS-1$
        data = getTextLayoutData();
        data.verticalAlignment = SWT.CENTER;
        lblTitle.setLayoutData(data);

        data = new GridData();
        data.grabExcessHorizontalSpace = true;
        data.horizontalAlignment = SWT.FILL;

        txtTitle = new Text(shell, SWT.BORDER);
        txtTitle.setLayoutData(data);

        lblDescription = new Label(shell, SWT.HORIZONTAL);
        lblDescription.setText(Messages
                .getString("juploadr.flickrrest.ui.photoset.description")); //$NON-NLS-1$
        lblDescription.setLayoutData(getTextLayoutData());

        data = new GridData();
        data.horizontalAlignment = SWT.FILL;
        data.heightHint = txtFieldHeight;
        data.widthHint = txtFieldWidth;

        txtDescription = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
        txtDescription.setLayoutData(data);

        data = new GridData();
        data.horizontalSpan = 2;
        data.grabExcessHorizontalSpace = true;
        buttons = new Composite(shell, SWT.NONE);
        FillLayout fillLayout = new FillLayout();
        fillLayout.spacing = 0;
        buttons.setLayout(fillLayout);
        data.horizontalAlignment = GridData.END;
        data.horizontalIndent = 0;
        buttons.setLayoutData(data);
        btnOk = new Button(buttons, SWT.PUSH);
        btnOk.setText(org.scohen.juploadr.Messages
                .getString("juploadr.ui.button.ok")); //$NON-NLS-1$
        btnOk.setSelection(true);

        btnCancel = new Button(buttons, SWT.PUSH);
        btnCancel.setText(org.scohen.juploadr.Messages
                .getString("juploadr.ui.button.cancel")); //$NON-NLS-1$

        SelectionAdapter listener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				NewPhotoSetDialog.this.buttonClicked(e);
			}
		};
        btnOk.addSelectionListener(listener);
        btnCancel.addSelectionListener(listener);
        shell.setDefaultButton(btnOk);
        shell.pack();
        shell.open();

        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        return;

    }

    /**
     * @param data
     */
    private GridData getTextLayoutData() {
        GridData data = new GridData();
        data.verticalIndent = 5;
        data.verticalAlignment = SWT.TOP;
        lblTitle.setLayoutData(data);
        return data;
    }

    protected void buttonClicked(SelectionEvent event) {

        if (event.widget == btnOk) {
            if (txtTitle.getText() == null
                    || txtTitle.getText().trim().length() == 0) {
                MessageUtils
                        .showErrorMessage(Messages
                                .getString("juploadr.flickrrest.ui.photoset.error.title.required")); //$NON-NLS-1$
            } else {
                photoSet = ImageUploadApi.getInstance().newPhotoSet(txtTitle.getText(), txtDescription.getText(), primary);
                if (photosToAdd != null && photosToAdd.size() > 0) {
                    for (int i = 0; i < photosToAdd.size(); i++) {
                        photoSet.addPhoto((ImageAttributes) photosToAdd.get(i));
                    }
                }
                photoSet.setNew(true);
                shell.dispose();
            }
        } else {
            shell.dispose();
        }

    }

    public PhotoSet getPhotoSet() {
        return photoSet;
    }
}
